<template>
  <common-layout>
    <div class="top">
      <div class="header">
        <span class="title">{{$t("login").toUpperCase()}}</span>
      </div>
    </div>
    <div class="login">
      <a-form @submit="onSubmit" :form="form">
        <a-form-item>
          <a-input
            autocomplete="autocomplete"
            size="large"
            placeholder="******@***.com"
            v-decorator="['Email', {rules: [{ required: true, whitespace: true},{ type: 'email' }]}]"
          >
            <a-icon slot="prefix" type="mail" />
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-input
            size="large"
            autocomplete="autocomplete"
            type="password"
            placeholder="********"
            v-decorator="['Password', {rules: [{ required: true, whitespace: true}]}]">
            <a-icon slot="prefix" type="lock" />
          </a-input>
        </a-form-item>
        <a-alert type="error" :closable="true" v-if="error" :message="error" @close='onClose' showIcon style="margin-bottom: 24px;" />
        <a-form-item>
          <a-button :loading="logging" style="width: 100%;margin-top: 24px" size="large" htmlType="submit" type="primary">{{$t("login")}}</a-button>
        </a-form-item>
      </a-form>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from '@/layouts/CommonLayout'
import {login, getRoutesConfig} from '@/services/user'
import {setAuthorization} from '@/utils/request'
import {loadRoutes} from '@/utils/routerUtil'
import {mapMutations} from 'vuex'

export default {
  name: 'Login',
  i18n: require('../i18n'),
  components: {CommonLayout},
  data () {
    return {
      logging: false,
      error: '',
      form: this.$form.createForm(this)
    }
  },
  methods: {
    ...mapMutations('account', ['setUser', 'setPermissions', 'setRoles']),
    onSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err) => {
        if (!err) {
          this.logging = true
          const email = this.form.getFieldValue('Email')
          const password = this.form.getFieldValue('Password')
          login(email, password).then(this.afterLogin)
        }
      })
    },
    afterLogin(res) {
      this.logging = false
      const loginRes = res.data
      if (loginRes.code >= 0) {
        const {user} = loginRes.data
        this.setUser(user)
        //this.setPermissions([{id: 'queryForm', operation: ['add', 'edit']}])
        //this.setRoles([{id: 'admin', operation: ['add', 'edit', 'delete']}])
        setAuthorization({token: loginRes.data.token, expireAt: new Date(new Date().getTime() + loginRes.data.expireAt * 60 * 1000)})
        // 获取路由配置
        getRoutesConfig().then(result => {
          const routesConfig = result.data.data
          loadRoutes(routesConfig)
          this.$router.push('/dashboard')
          this.$message.success(this.$t("loginSuccess"), 3)
        })
      } else {
        this.error = loginRes.message
      }
    },
    onClose() {
      this.error = false
    }
  }
}
</script>

<style lang="less" scoped>
  .common-layout{
    .top {
      text-align: center;
      margin-bottom: 50px;
      .header {
        height: 44px;
        line-height: 44px;
        a {
          text-decoration: none;
        }
        .logo {
          height: 44px;
          vertical-align: top;
          margin-right: 16px;
        }
        .title {
          font-size: 33px;
          color: @title-color;
          font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
          font-weight: 600;
          position: relative;
          top: 2px;
        }
      }
    }
    .login{
      width: 368px;
      margin: 0 auto;
      @media screen and (max-width: 576px) {
        width: 95%;
      }
      @media screen and (max-width: 320px) {
        .captcha-button{
          font-size: 14px;
        }
      }
      .icon {
        font-size: 24px;
        color: @text-color-second;
        margin-left: 16px;
        vertical-align: middle;
        cursor: pointer;
        transition: color 0.3s;

        &:hover {
          color: @primary-color;
        }
      }
    }
  }
</style>
